/*
1:定义home模块
2：在根模块下应用
3：定义state与action,且actions调用接口获取数据
4：home页banner组件内触发获取数据
5：actions中存储数据(数据类型定义)
6：渲染数据
*/
import { defineStore } from 'pinia'
import request from '@/utils/request'
import {
  BannerItem,
  ApiRes,
  NewItem,
  HotItem,
  BrandItem,
  ProductItem,
  SpecialItem
} from '@/types/data.d'
export default defineStore('home', {
  state () {
    return {
      bannerList: [] as BannerItem[],
      newList: [] as NewItem[], // 新鲜好物
      hotList: [] as HotItem[], // 热门推荐
      brandList: [] as BrandItem[], // 热门品牌
      productList: [] as ProductItem[], // 商品区块
      specialList: [] as SpecialItem[] // 专题列表
    }
  },
  actions: {
    // 获取banner数据
    async homeBanner () {
      if (this.bannerList.length === 0) {
        const res = await request.request<ApiRes<BannerItem[]>>({
          url: '/home/banner'
        })
        this.bannerList = res.data.result
        console.log('homeBanner', res)
      }
    },
    // 获取新鲜好物数据
    async homeNew () {
      const res = await request.request<ApiRes<NewItem[]>>({
        url: '/home/new'
      })
      this.newList = res.data.result
      console.log('新鲜好物', res)
    },
    // 获取人气推荐数据
    async homeHot () {
      const res = await request.request<ApiRes<HotItem[]>>({
        url: '/home/hot'
      })
      this.hotList = res.data.result
      console.log('人气推荐', res)
    },
    // 获取热门品牌数据
    async homeBrand (limit: number = 10) {
      const res = await request.request<ApiRes<BrandItem[]>>({
        url: '/home/brand',
        params: {
          limit: limit
        }
      })
      this.brandList = res.data.result
      console.log('homeBrand', res)
    },
    // 获取商品区块数据
    async homeGoods () {
      const res = await request.request<ApiRes<ProductItem[]>>({
        url: '/home/goods'
      })
      this.productList = res.data.result
      console.log('获取商品区块数据', res)
    },
    // 获取最新专题
    async homeSpecial () {
      const res = await request.request<ApiRes<SpecialItem[]>>({
        url: '/home/special'
      })
      this.specialList = res.data.result
      console.log('获取最新专题', res)
    }
  }
})
